
 begintownscript;

 variables;

 int i,j,k,r1,choice;

 body;

 beginstate INIT_STATE;

	set_level(29,45);

	if (get_flag(17,17) >= 1) {
		set_terrain(17,46,0);
		set_terrain(17,45,0);
		set_terrain(16,46,0);
		set_terrain(18,46,0);
}

	if (get_flag(17,9) == 1)
		set_terrain(17,49,447);
break;

 beginstate START_STATE;

	if (get_flag(17,4) == 1) {
		message_dialog("Vitendo lets out a pained cry. He was no match for the demons. He slumps lifeless to the ground and with him die the hopes of the rebels for a free Faerengrove.","You can't say you feel sorry for him, he brought this on himself.");
		set_flag(17,4,2);
}
	if ((get_flag(17,2) == 2) && (char_ok(13) == FALSE) && (char_ok(14) == FALSE) && (char_ok(16) == FALSE) && (char_ok(17) == FALSE) && (char_ok(18) == FALSE)) {
		set_flag(17,2,5);
		message_dialog("All of the rebels have been slaughtered. Not that they were much of a help, but now you must fight all of the demons by yourselves.","");
}

	if ((get_flag(66,6) == 1) && (char_ok(13) == TRUE || char_ok(14) == TRUE || char_ok(16) == TRUE || char_ok(17) == TRUE || char_ok(18) == TRUE)) {
		message_dialog("The surviving rebels gather their wounded and head for the exit. One of them grants you a few words. _We can't thank ye enough for sorting out this mess. Regrettably, I think we should lay low for a while._","_Rebellion will continue another day._ Then the rebels depart.");
		set_flag(66,6,2);
		set_flag(17,2,5);
		erase_char(13);
		erase_char(14);
		erase_char(16);
		erase_char(17);
		erase_char(18);
}
break;
		
 beginstate EXIT_STATE;

break;

 beginstate 10;
	move_to_new_town(16,32,16);
break;

 beginstate 11;
	if (get_flag(17,0) == 0) {
		message_dialog("Looks like the statues on the middle weren't statues after all. They began to move immediately when you moved close enough, revealing what they really are, golems.","");
		set_terrain(31,15,0);
		set_terrain(31,20,0);
force_instant_terrain_redraw();
		place_monster(31,15,220,0);
		place_monster(31,20,220,0);
		set_flag(17,0,1);
}
break;

 beginstate 12;
	if (get_flag(17,1) == 0) {
		message_dialog("There is a soft click when you step on the mosaic floor. The tiles on your both sides fall down into a sizzling pool of lava. The tiles are rigged.","");
		set_flag(17,1,1);
}
	set_floor(11,24,79);
	set_height(11,24,3);
	set_floor(13,24,79);
	set_height(13,24,3);
	play_sound(9);
	play_sound(-5);
force_instant_terrain_redraw();

break;

 beginstate 13;
	message_dialog("The tile underneath you disappears with a click, dropping your entire party into the merciless lava pool. At least your deaths were quick.","");
	kill_char(1000,2,0);
break;

 beginstate 14;
	set_floor(14,25,79);
	set_height(14,25,3);
	set_floor(10,25,79);
	set_height(10,25,3);
	play_sound(9);
	play_sound(-5);
force_instant_terrain_redraw();

break;

 beginstate 17;
	set_floor(11,26,79);
	set_height(11,26,3);
	set_floor(12,26,79);
	set_height(12,26,3);
	set_floor(13,26,79);
	set_height(13,26,3);
	play_sound(9);
	play_sound(-5);
force_instant_terrain_redraw();

break;

 beginstate 18;
	set_floor(11,27,79);
	set_height(11,27,3);
	set_floor(13,27,79);
	set_height(13,27,3);
	play_sound(9);
	play_sound(-5);
force_instant_terrain_redraw();

break;

 beginstate 21;
	set_floor(10,28,79);
	set_height(14,28,3);
	set_floor(10,28,79);
	set_height(10,28,3);
	play_sound(9);
	play_sound(-5);
force_instant_terrain_redraw();

break;

 beginstate 22;
	set_floor(11,29,79);
	set_height(11,29,3);
	set_floor(13,29,79);
	set_height(13,29,3);
	play_sound(9);
	play_sound(-5);
force_instant_terrain_redraw();


break;

 beginstate 24;
	if (get_flag(17,2) == 0) {
		set_flag(17,2,1);
		block_entry(1);
		force_view_center(28,42);
		march_party(23,42);
force_instant_terrain_redraw();
		pause(3);
		march_party(24,42);
force_instant_terrain_redraw();
		pause(3);
		march_party(25,42);
force_instant_terrain_redraw();
		pause(3);
		march_party(26,42);
force_instant_terrain_redraw();
		pause(3);
		march_party(27,42);
force_instant_terrain_redraw();
		pause(3);
force_instant_terrain_redraw();
		relocate_character(0,27,41);
		relocate_character(1,27,43);
		relocate_character(2,26,42);
		relocate_character(3,25,42);
force_instant_terrain_redraw();
		pause(3);
		reset_dialog();
		add_dialog_str(0,"The demon lord Kaejil Nyh'yltak doesn't even seem to notice your presence. He is holding a tiny human skull between his massive fingers. The demon lord is over ten feet tall and on his back he has enormous black wings.",0);
		add_dialog_str(1,"By his side the demon lord has a pitch black blade. The demon raises its eyes to stare at you. It would appear the haakai wished to speak with you.",0);
		add_dialog_choice(0,"Listen to the demon.");
		add_dialog_choice(1,"Charge the demon straight on.");
		choice = run_dialog(1);
		if (choice == 1) {
		reset_dialog();
		add_dialog_str(0,"Then the haakai decides to speak to you in a calm booming voice. _You have been quite the nuisance. Storming my stronghold and making this far is commendable, for mortals._",0);
		if (get_flag(3,10) > 0)
			add_dialog_str(1,"_You slaughtered the goblins who were drinking from our power. They were weak, disposable critters._",0);
		if (get_flag(5,13) > 0)
			add_dialog_str(2,"_You defeated Marnok's slithzerikai. He was most displeased about that. But they were nothing but brute strength, not a force to be reckoned with._",0);
		if (has_special_item(0))
			add_dialog_str(3,"_You even turned that vampire against me. Though I believe he was most willing to do so. He will be the first to die, after you have been disposed of._",0);
		if (get_flag(8,1) >= 3)
			add_dialog_str(4,"_Even the vampire was no match for you. He was weak, the undead I provided were his only true asset._",0);
		if ((get_flag(100,1) == 5) || (get_flag(100,1) == 9))
		add_dialog_str(5,"_You banished Marnok. For that reason only, you should not be underestimated. He was my right hand, but perhaps he was too weak._",0);
		add_dialog_choice(0,"OK.");
		run_dialog(1);
		message_dialog("_But now your time has come. You will be butchered and your bones shall decorate my walls._ The demon grabs his mighty great sword. _Farewell mortals._","You noticed that the demon amulet had been heating up during the demon's speech. It now burns your skin. When the haakai takes a step closer a bright ray shoots from the amulet. The haakai grimaces. _What was that!?_");
		pause(5);
		text_bubble_on_char(6,"You!");
force_instant_terrain_redraw();
		activate_hidden_group(1);
		put_boom_on_char(11,2,0);
		put_boom_on_char(12,2,0);
		run_animation_sound(10);
		pause(6);
		text_bubble_on_char(6,"");
		text_bubble_on_char(6,"Will die!");
force_instant_terrain_redraw();
		pause(8);
		set_attitude(7,10);
		set_attitude(8,10);
		set_attitude(9,10);
		set_attitude(10,10);
	if (get_flag(13,6) == 1 && get_flag(13,5) < 5) {
		text_bubble_on_char(6,"");
		text_bubble_on_char(6,"?");
force_instant_terrain_redraw();
		activate_hidden_group(2);
		put_boom_on_char(13,2,0);
		put_boom_on_char(14,2,0);
		put_boom_on_char(15,2,0);
		put_boom_on_char(16,2,0);
		put_boom_on_char(17,2,0);
		put_boom_on_char(18,2,0);
		run_animation_sound(10);
		pause(6);
		text_bubble_on_char(6,"");
		text_bubble_on_char(15,"I'm a wizard!");
force_instant_terrain_redraw();
		pause(10);
		set_character_pose(6,1);
force_instant_terrain_redraw();
		put_straight_zap(32,42,28,42,5);
		put_effect_on_char(15,12,1,2);
		run_animation_sound(53);
force_instant_terrain_redraw();
		pause(2);
		text_bubble_on_char(15,"");
force_instant_terrain_redraw();
		run_animation_sound(170);
		kill_char(15,2,0);
		pause(8);
		text_bubble_on_char(6,"Indeed.");
		set_character_pose(6,0);
		set_character_facing(14,6);
force_instant_terrain_redraw();
		pause(8);
		text_bubble_on_char(6,"");
		text_bubble_on_char(14,"Damn it!");
force_instant_terrain_redraw();
		pause(9);
		text_bubble_on_char(14,"");
		text_bubble_on_char(14,"Don't just stand there!");
force_instant_terrain_redraw();
		pause(9);
		text_bubble_on_char(14,"");
		text_bubble_on_char(14,"Fight the demon!");
force_instant_terrain_redraw();
		pause(8);
		set_character_facing(14,2);
force_instant_terrain_redraw();
		text_bubble_on_char(14,"");
		pause(10);
		set_level(13,40);
		set_level(14,30);
		set_level(16,31);
		set_level(17,30);
		set_name(13,"Solid");
		set_name(14,"Vitendo");
		set_name(16,"Asp");
		set_name(17,"Mercenary");
		set_name(18,"Rebel");
		set_flag(17,2,2);

	end();
}
}

	if (choice == 2)
		set_state_continue(36);
}
break;

 beginstate 25;
	if (get_flag(17,9) == 0) {
		reset_dialog_preset_options(3);
		add_dialog_str(0,"The portal is glowing wildly. You see no power sources that you could disable or any other means of disabling the portal.",0);
		if (has_special_item(0))
			add_dialog_str(1,"Then you feel it. The amulet Mosadorl gave you is burning hot. You take the amulet out of your pocket and hold it by the chain. The closer you take it to the portal, the brighter it glows.",0);
		if (has_special_item(10))
			add_dialog_str(2,"Then you feel it. The amulet Bearworth gave you is burning hot. You take the amulet out of your pocket and hold it by the chain. The closer you take it to the portal, the brighter it glows.",0);
		add_dialog_choice(2,"Hurl the amulet into the portal.");
		choice = run_dialog(1);
	if (choice == 3)
		set_state_continue(31);

	if (choice == 2)
		set_state_continue(33);
}
 break;

 beginstate 26;
	if (get_flag(17,3) == 0) {
		message_dialog("The architecture of this subterranean structure is completely different from the fortress above it. Where the fort was clearly made by humans, these halls look somehow warped.","The shapes on the walls are almost hypnotic. It seems tempting just to stand here and stare at the walls.");
		set_flag(17,3,1);
}
 break;

 beginstate 27;
	if (get_flag(17,5) == 0) {
		message_dialog("The demon structure is clearly sill under construction. The infernals have dug a hole in the cave walls, in order to enlarge the structure itself.","Then you realize something, the infernal troopers aren't soldiers, they are workers.");
		set_flag(17,5,1);
}
 break;

 beginstate 28;
	if (get_flag(17,6) == 0) {
		reset_dialog();
		add_dialog_str(0,"Searching the shelves you find an interesting scroll. Sinister runes cover it, but if you understand correctly, it contains the information on a spell of some sort.",0);
		add_dialog_str(1,"You could try to decipher the scroll, if you wish.",0);
		add_dialog_choice(0,"Try to read the runes.");
		add_dialog_choice(1,"Leave."); 
		choice = run_dialog(1);

		if ((choice == 1) && (get_skill_total(42) >= 50)) 
			set_state_continue(29);

		if ((choice == 1) && (get_skill_total(42) < 50)) 
			message_dialog("Alas, you cannot make any sense out of the runes.","");

 break;

 beginstate 29;
	message_dialog("You manage to make some sense out of the strange runes. They help you improve your forcecage spell.","");
			j = get_selected_pc();
			i = 0;
			while (i < 4) {
			if (((j < 0) || (j == i)) && (char_ok(i))) {
				set_flag(17,6,1);
				change_spell_level(i,0,15,1);
		}
		i = i + 1;
	}
}
 break;

 beginstate 30;
	if (get_flag(17,8) == 0) {
		message_dialog("Very powerful looking magic barriers block your entry to one of the rooms. The barriers are unlike any barrier you've ever encountered, but they are transparent. You take a look to see what lies beyond them.","You see a glowing portal.");
		set_flag(17,8,1);
}

	if (get_flag(17,17) == 1) {
		message_dialog("The barriers to the portal room fade away. Apparently they drew power from Kaejil Nyh'yltak himself. The portal no doubt leads to the abyssal planes and it is imperative that you find a way to disable the portal.","If you fail the demon lord might find an easy way back to our realm.");
		block_entry(1);
		set_flag(17,17,2);
		set_terrain(17,46,0);
		set_terrain(17,45,0);
		set_terrain(16,46,0);
		set_terrain(18,46,0);
		pause(6);
force_instant_terrain_redraw();
		march_party(18,44);
force_instant_terrain_redraw();
		pause(3);
		march_party(17,45);
force_instant_terrain_redraw();
		pause(3);
		march_party(17,46);
force_instant_terrain_redraw();
		pause(3);
		march_party(17,47);
force_instant_terrain_redraw();
		pause(3);
		march_party(17,48);
force_instant_terrain_redraw();
		pause(3);
		set_state_continue(25);
}
 break;

 beginstate 31;
	if (has_special_item(0)) {
		message_dialog("As soon as the amulet hits the teleport a huge electrical storm erupts around the portal. You automatically jump back to watch the display. Just when it looks like the whole portal might explode, the storm just stops.","You look at the portal, it is just an empty arch now. You've done it! You stopped Kaejil Nyh'yltak!");
		set_flag(17,9,1);
		set_terrain(17,49,447);
}
	if (has_special_item(10)) {
		message_dialog("As soon as the amulet hits the teleport a huge electrical storm erupts around the portal. You automatically jump back to watch the display. The teleport explodes causing the electrical storm to hit your party.","It is extremely painful.");
		set_flag(17,9,1);
		set_terrain(17,49,447);
		damage_char(1000,100,3);
}
 break;

 beginstate 32;
	if ((get_flag(17,17) == 2) && (char_ok(29) == TRUE)) {
		relocate_character(29,12,30);
		put_boom_on_char(29,2,0);
force_instant_terrain_redraw();
		run_animation_sound(10);
		if (get_flag(100,1) != 0)
			message_dialog("The demon Marnok appears in front of you grinning maliciously. _You banished my master, congratulations. But now in your weakened state, I will get revenge for the humiliations!_","Marnok attacks you screaming madly.");
		if (get_flag(100,1) == 0)
			message_dialog("A mung demon appears in front of you grinning maliciously. _Congratulations mortals! I witnessed your fight from afar. You banished my master. That makes me in charge._","_As a token of my appreciation I will kill you quickly!_");
		set_flag(17,17,5);
		set_flag(100,1,5);
}
 break;

 beginstate 33;
	message_dialog("Stepping into the portal was the dumbest thing you've ever done. Not surprisingly the portal leads to the infernal planes, with millions of demons ready to tear you limb from limb.","You last five seconds.");
	kill_char(1000,2,0);
 break;

 beginstate 34;
	if (get_flag(17,10) < 2) {
		reset_dialog();
		add_dialog_str(0,"In front of you stands a pedestal. It has a strange magical barrier on top of it and inside the barrier, there is a bracelet.",0);
		add_dialog_str(1,"You lean close to examine the barrier. Then a voice booms from the pedestal. It is Marnok's. _How fortunate. Someone wants to have my bracelet. Then answer me this._",0);
		add_dialog_str(2,"_What is this thing that having it, You can no longer give it away, But lacking it, for the moment at least, You can give it to those who must pay?_ A riddle, answering correctly may remove the barrier. What do you do?",0);
		add_dialog_choice(0,"Answer.");
		add_dialog_choice(1,"Do not answer, leave.");
		choice = run_dialog(1);

	if (choice == 1) {
		get_text_response("Your answer is?");
		check_text_response_match("Death");
	if (got_text_match()) {
			set_flag(17,10,2);
			message_dialog("As you speak the word, the barrier disappears. You pocket the bracelet which is definitely magical.","");
			reward_give(457);
}
	if ((got_text_match() == FALSE) && (get_flag(17,10) == 0)) {
		message_dialog("As you speak the word, a booming laughter erupts from the pedestal. Three powerful looking shades have materialized behind the pedestal.","");
		activate_hidden_group(3);
		set_flag(17,10,1);
		end();
}

	if ((got_text_match() == FALSE) && (get_flag(17,10) == 1))
		message_dialog("The barrier remains but at least there are no shades this time.","");
}
}
 break;

 beginstate 35;
	block_entry(1);
	message_dialog("You gaze at the bubbling pit of lava. Finding nothing of interest you back away. Careless adventurers might fall in - not you, though.","");
 break;

 beginstate 36;

message_dialog("The demon lord lets out a growling laughter and flaps his mighty wings as you attack. You try to get to the demon lord as quickly as you can.","Your attack comes to a quick halt, however. As you approached the demon lord the tinkered amulet quickly heated up. It now burns your skin. When the haakai takes a step closer a bright ray shoots from the amulet. The haakai grimaces. _What was that!?_");
		pause(5);
		text_bubble_on_char(6,"You!");
force_instant_terrain_redraw();
		activate_hidden_group(1);
		put_boom_on_char(11,2,0);
		put_boom_on_char(12,2,0);
		run_animation_sound(10);
		pause(6);
		text_bubble_on_char(6,"");
		text_bubble_on_char(6,"Will die!");
force_instant_terrain_redraw();
		pause(8);
		set_attitude(7,10);
		set_attitude(8,10);
		set_attitude(9,10);
		set_attitude(10,10);
	if (get_flag(13,6) == 1 && get_flag(13,5) < 5) {
		text_bubble_on_char(6,"");
		text_bubble_on_char(6,"?");
force_instant_terrain_redraw();
		activate_hidden_group(2);
		put_boom_on_char(13,2,0);
		put_boom_on_char(14,2,0);
		put_boom_on_char(15,2,0);
		put_boom_on_char(16,2,0);
		put_boom_on_char(17,2,0);
		put_boom_on_char(18,2,0);
		run_animation_sound(10);
		pause(6);
		text_bubble_on_char(6,"");
		text_bubble_on_char(15,"I'm a wizard!");
force_instant_terrain_redraw();
		pause(10);
		set_character_pose(6,1);
force_instant_terrain_redraw();
		put_straight_zap(32,42,28,42,5);
		put_effect_on_char(15,12,1,2);
		run_animation_sound(53);
force_instant_terrain_redraw();
		pause(2);
		text_bubble_on_char(15,"");
force_instant_terrain_redraw();
		run_animation_sound(170);
		kill_char(15,2,0);
		pause(8);
		text_bubble_on_char(6,"Indeed.");
		set_character_pose(6,0);
		set_character_facing(14,6);
force_instant_terrain_redraw();
		pause(8);
		text_bubble_on_char(6,"");
		text_bubble_on_char(14,"Damn it!");
force_instant_terrain_redraw();
		pause(9);
		text_bubble_on_char(14,"");
		text_bubble_on_char(14,"Don't just stand there!");
force_instant_terrain_redraw();
		pause(9);
		text_bubble_on_char(14,"");
		text_bubble_on_char(14,"Fight the demon!");
force_instant_terrain_redraw();
		pause(8);
		set_character_facing(14,2);
force_instant_terrain_redraw();
		text_bubble_on_char(14,"");
		pause(10);
		set_level(13,40);
		set_level(14,30);
		set_level(16,31);
		set_level(17,30);
		set_name(13,"Solid");
		set_name(14,"Vitendo");
		set_name(16,"Asp");
		set_name(17,"Mercenary");
		set_name(18,"Rebel");
		set_flag(17,2,2);

	end();
}

 break;
	